package com.qst.service.ssm;

import com.qst.ExamException;
import com.qst.entity.PersonalityDimension;
import com.qst.mapper.DimensionMapper;
import com.qst.service.DimensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@SuppressWarnings("all")
public class DimensionServiceSsmImpl implements DimensionService {

   @Autowired
   DimensionMapper dimensionMapper;

    @Override
    public List<PersonalityDimension> findDimensionByAssessment(int id) {
        return dimensionMapper.findByAssessment(id);
    }
    @Override
    public PersonalityDimension findDimensionById(int id) {
        return dimensionMapper.findById(id);
    }
    @Override
    public void updateDimension(PersonalityDimension kp) {
        PersonalityDimension temp = dimensionMapper.findByAssessmentAndTitle(kp.getAssessmentId(), kp.getTitle());
        if (temp == null || temp.getId() == kp.getId()) {
            dimensionMapper.update(kp);
        } else {
            throw new ExamException("本性格维度名称已存在");
        }
    }

    @Override
    public void saveDimension(PersonalityDimension kp) {
        PersonalityDimension temp = dimensionMapper.findByAssessmentAndTitle(kp.getAssessmentId(), kp.getTitle());
        if (temp == null) {
            dimensionMapper.insert(kp);
        } else {
            throw new ExamException("性格维度名称已存在");
        }
    }

    @Override
    public void deleteDimension(int id) {
        List<PersonalityDimension>  personalityDimensionList = dimensionMapper.findCountByDimension(id);
         Integer count= personalityDimensionList.size();
        if (count > 0) {
            throw new ExamException("本知识点已录入考题，不能再删除");
        }
        dimensionMapper.delete(id);
    }
}
